﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Qplus.Infrastructure;
using Qplus.protocol;
using Qplus.enums;
using System.Diagnostics;
using Qplus.beans;

namespace Qplus.UI
{
    public class ChatListener : IPacketListener
    {
        public bool isInSession = false;

        private static ChatListener instance;

        public event EventHandler<UIEvent> U2C_RESP_SESSION_BEGIN_EVENT;
        public event EventHandler<UIEvent> U2C_NOTIFY_SESSION_BEGIN_EVENT;
        public event EventHandler<UIEvent> U2C_RESP_CHAT_SENDMSG_EVENT;
        public event EventHandler<UIEvent> U2C_NOTIFY_CHAT_RECVMSG_EVENT;
        public event EventHandler<UIEvent> msgEvent;
        public event EventHandler<UIEvent> friendMsgEvent;
        public event EventHandler<UIEvent> strangerMsgEvent;

        public ChatListener()
        {

        }

        public static ChatListener Instance
        {
            get
            {
                if (null == instance)
                    instance = new ChatListener();
                return instance;
            }
            set { instance = value; }
        }

        public virtual void packetEvent(PacketEvent Pevent)
        {
            InPacket packet = Pevent.myPacket;
            if (packet == null)
            {
                return;
            }
            else
            {
                int id = packet.getMsgID();
                MMessageID MID = (MMessageID)packet.getMsgID();
                switch ((MMessageID)packet.getMsgID())
                {
                    case MMessageID.U2C_RESP_SESSION_BEGIN:
                        {
                            Debug.WriteLine("U2C_RESP_SESSION_BEGIN,开始单聊");
                            U2C_RESP_SESSION_BEGIN p = ((U2C_RESP_SESSION_BEGIN)packet);
                            QUser.Instance.session.appID = p.getAppID();
                            QUser.Instance.session.sessionID = p.getSessionID();
                            QUser.Instance.session.friendID = p.getFriendID();
                            QUser.Instance.session.result = p.getResult();
                            if (null != U2C_RESP_SESSION_BEGIN_EVENT)
                                U2C_RESP_SESSION_BEGIN_EVENT(this, new UIEvent());
                            break;
                        }
                    case MMessageID.U2C_NOTIFY_SESSION_BEGIN:
                        {
                            if (null != U2C_NOTIFY_SESSION_BEGIN_EVENT)
                                U2C_NOTIFY_SESSION_BEGIN_EVENT(this, new UIEvent());
                            break;
                        }
                    case MMessageID.U2C_RESP_SESSION_END:
                        {
                            break;
                        }
                    case MMessageID.U2C_NOTIFY_SESSION_END:
                        {
                            break;
                        }
                    case MMessageID.U2C_RESP_CHAT_SENDMSG:
                        {
                            Debug.WriteLine("U2C_RESP_CHAT_SENDMSG,发送消息回执");

                            U2C_RESP_CHAT_SENDMSG p = ((U2C_RESP_CHAT_SENDMSG)packet);
                            long appID=p.getAppID();
                            long sessionID=p.getSessionID();
                            int result=p.getResult();
                            if (null != U2C_RESP_CHAT_SENDMSG_EVENT)
                                U2C_RESP_CHAT_SENDMSG_EVENT(this, new UIEvent(p));
                            break;
                        }
                    case MMessageID.U2C_NOTIFY_CHAT_RECVMSG:
                        {
                            Debug.WriteLine("U2C_NOTIFY_CHAT_RECVMSG,收到消息");

                            //processMsgEvent();
                            if (null != U2C_NOTIFY_CHAT_RECVMSG_EVENT)
                                U2C_NOTIFY_CHAT_RECVMSG_EVENT(this, new UIEvent(packet));
                            break;
                        }
                    default:
                        break;
                }
            }
        }
    }
}
